<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png?v=7.4.0">
  <link rel="mask-icon" href="/images/avatar.svg?v=7.4.0" color="#222">
  <link rel="alternate" href="/atom.xml" title="Anemone's Blog" type="application/atom+xml">
  <meta name="google-site-verification" content="Re5JdegRYzNFco-rC9lYIsvSWIgh5JvyfhuEaZCeFCk">
  <meta name="baidu-site-verification" content="opTC8YN3Pn">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: 'GB90MXPJ1C',
      apiKey: '05d808da3baf50ac2f2fad2dc3a3cd8f',
      indexName: 'dev_blog',
      hits: {"per_page":20},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="简介内容安全策略   (CSP) 是一个额外的安全层，用于允许站点的管理者控制页面上哪些资源能够被用户代理程序加载。以阻止包括跨站脚本 (XSS) 、数据注入等攻击。使用在HTTP头部指定策略1Content-Security-Policy: policy使用meta标签指定策略">
<meta name="keywords" content="HTTP,CSP,协议安全">
<meta property="og:type" content="article">
<meta property="og:title" content="内容安全策略CSP学习笔记">
<meta property="og:url" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/index.html">
<meta property="og:site_name" content="Anemone&#39;s Blog">
<meta property="og:description" content="简介内容安全策略   (CSP) 是一个额外的安全层，用于允许站点的管理者控制页面上哪些资源能够被用户代理程序加载。以阻止包括跨站脚本 (XSS) 、数据注入等攻击。使用在HTTP头部指定策略1Content-Security-Policy: policy使用meta标签指定策略">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548835133910.png">
<meta property="og:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548835338494.png">
<meta property="og:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548835861387.png">
<meta property="og:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548837928332.png">
<meta property="og:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548838217202.png">
<meta property="og:updated_time" content="2019-09-22T10:14:18.561Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="内容安全策略CSP学习笔记">
<meta name="twitter:description" content="简介内容安全策略   (CSP) 是一个额外的安全层，用于允许站点的管理者控制页面上哪些资源能够被用户代理程序加载。以阻止包括跨站脚本 (XSS) 、数据注入等攻击。使用在HTTP头部指定策略1Content-Security-Policy: policy使用meta标签指定策略">
<meta name="twitter:image" content="http://anemone.top/HTTP-内容安全策略CSP学习笔记/1548835133910.png">
  <link rel="canonical" href="http://anemone.top/HTTP-内容安全策略CSP学习笔记/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>内容安全策略CSP学习笔记 | Anemone's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Anemone's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="fa fa-search fa-fw"></i>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input" id="search-input"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

  
</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="http://anemone.top/HTTP-内容安全策略CSP学习笔记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Anemone">
      <meta itemprop="description" content="关注Web安全、移动安全、Fuzz测试和机器学习">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Anemone's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">内容安全策略CSP学习笔记

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-01-30 14:45:51" itemprop="dateCreated datePublished" datetime="2019-01-30T14:45:51+08:00">2019-01-30</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-22 18:14:18" itemprop="dateModified" datetime="2019-09-22T18:14:18+08:00">2019-09-22</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Web安全-HTTP/" itemprop="url" rel="index"><span itemprop="name">Web安全-HTTP</span></a></span>

                
                
              
            </span>
          

          
            <span id="/HTTP-内容安全策略CSP学习笔记/" class="post-meta-item leancloud_visitors" data-flag-title="内容安全策略CSP学习笔记" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>内容安全策略   (CSP) 是一个额外的安全层，用于允许站点的管理者控制页面上哪些资源能够被用户代理程序加载。以阻止包括跨站脚本 (XSS) 、数据注入等攻击。</p><h1 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h1><h2 id="在HTTP头部指定策略"><a href="#在HTTP头部指定策略" class="headerlink" title="在HTTP头部指定策略"></a>在HTTP头部指定策略</h2><figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy</span>: policy</span><br></pre></td></tr></table></figure><h2 id="使用meta标签指定策略"><a href="#使用meta标签指定策略" class="headerlink" title="使用meta标签指定策略"></a>使用meta标签指定策略</h2><a id="more"></a>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"content-security-policy"</span> <span class="attr">content</span>=<span class="string">"policy"</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h1><h2 id="存在漏洞的服务器"><a href="#存在漏洞的服务器" class="headerlink" title="存在漏洞的服务器"></a>存在漏洞的服务器</h2><p>写一个反射式的XSS服务吧：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php echo $_GET[&apos;xss&apos;];?&gt;</span><br><span class="line">&lt;script src=&quot;/js/benign.js&quot;&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p>bengin.js:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="string">"I'm benign~~~"</span>);</span><br></pre></td></tr></table></figure>
<p>evil.js:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="string">"I'm evil!"</span>);</span><br></pre></td></tr></table></figure>
<p>毫无疑问它是存在漏洞的，不过无奈现在chrome的反射型xss防御做的太好了，我们只能先暂时关闭它(j加header)来降低实验难度，在BP里面加一个”X-XSS-Protection:0”的Header：</p>
<p><img src="/HTTP-内容安全策略CSP学习笔记/1548835133910.png" alt="1548835133910"></p>
<p>我在8000端口又起了一个http服务，接着随心所欲的XSS：</p>
<p><img src="/HTTP-内容安全策略CSP学习笔记/1548835338494.png" alt="1548835338494"></p>
<h2 id="应用默认策略"><a href="#应用默认策略" class="headerlink" title="应用默认策略"></a>应用默认策略</h2><p>配置该站点的内容均来自同源：</p>
<figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy</span>: default-src 'self'</span><br></pre></td></tr></table></figure>
<p>其中，<code>default-src</code>是指若其他元素来源的规则（如<code>script-src</code>，<code>img-src</code>）没有设置的情况下，默认使用<code>default-src</code>的配置，<code>self</code>是同源。</p>
<p>设置后可以看到xss被拦截，以为恶意脚本来自不同的源：</p>
<p><img src="/HTTP-内容安全策略CSP学习笔记/1548835861387.png" alt="1548835861387"></p>
<h2 id="信任多个域名"><a href="#信任多个域名" class="headerlink" title="信任多个域名"></a>信任多个域名</h2><p>元素的可信来源可以有多个，比如：</p>
<figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy</span>: default-src 'self' *.trusted.com</span><br></pre></td></tr></table></figure>
<h2 id="来源配置"><a href="#来源配置" class="headerlink" title="来源配置"></a>来源配置</h2><ul>
<li><code>*</code>： 允许所有</li>
<li><code>none</code>：禁止加载任何路径的资源</li>
<li><code>self</code>：允许加载同源的资源</li>
<li><code>data:</code>：允许通过数据模式加载资源</li>
<li><code>domain.ccc.com</code>：允许加载匹配域名的资源</li>
<li><code>*.ccc.com</code>：允许加载匹配域名的资源</li>
<li><code>https://img.ccc.com</code>：允许加载https方式的域名资源</li>
<li><code>unsafe-inline</code>：允许使用内联元素</li>
<li><code>unsafe-eval</code>：允许执行eval函数</li>
</ul>
<h2 id="精确控制"><a href="#精确控制" class="headerlink" title="精确控制"></a>精确控制</h2><p>其他可以设置的源有：</p>
<ul>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/child-src" target="_blank" rel="noopener"><code>child-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src" target="_blank" rel="noopener"><code>connect-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/font-src" target="_blank" rel="noopener"><code>font-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src" target="_blank" rel="noopener"><code>frame-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/img-src" target="_blank" rel="noopener"><code>img-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/manifest-src" target="_blank" rel="noopener"><code>manifest-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/media-src" target="_blank" rel="noopener"><code>media-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/object-src" target="_blank" rel="noopener"><code>object-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/script-src" target="_blank" rel="noopener"><code>script-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/style-src" target="_blank" rel="noopener"><code>style-src</code></a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src" target="_blank" rel="noopener"><code>worker-src</code></a></li>
<li><code>sandbox allow-forms allow-scripts</code>：授权一个沙箱来请求iframe sanbox等类似属性的资源，沙箱默认使用同源策略，可增加配置，如<code>allow-scripts</code>，允许跨站script</li>
</ul>
<p>例如，信任任意的图片来源，但是只信任本站的其他来源：</p>
<figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy</span>: default-src 'self'; img-src *</span><br></pre></td></tr></table></figure>
<h2 id="内嵌脚本"><a href="#内嵌脚本" class="headerlink" title="内嵌脚本"></a>内嵌脚本</h2><p>新版的默认策略已经封禁了内嵌脚本的执行，这意味着即使我们不引入外部js，而直接在<code>&lt;script&gt;</code>标签里面写东西也是不允许的（这也造成<a href="https://www.anquanke.com/post/id/151496#h2-4" target="_blank" rel="noopener">巧妙地绕过CSP：欺骗CSP执行任意代码</a>一文的失效：</p>
<p><img src="/HTTP-内容安全策略CSP学习笔记/1548837928332.png" alt="1548837928332"></p>
<p>那么正常的需求怎么办呢？正常服务器发送网页的时候，需要在策略中告诉浏览器一个随机生成的token。</p>
<figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy</span>: script-src 'nonce-EDNnf03nceIOfn39fn3e9h3sdfa'</span><br></pre></td></tr></table></figure>
<p>页面内嵌脚本必须有这个token才能执行：<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">nonce</span>=<span class="string">EDNnf03nceIOfn39fn3e9h3sdfa</span>&gt;</span><span class="javascript"> <span class="comment">// some code </span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h1 id="测试策略"><a href="#测试策略" class="headerlink" title="测试策略"></a>测试策略</h1><p>可以将CSP配置为仅报告模式，这样即使出现违规，CSP也只是报告这个行为而不强制拦截：</p>
<figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Content-Security-Policy-Report-Only</span>: default-src 'self'; report-uri http://127.0.0.1:8000</span><br></pre></td></tr></table></figure>
<p><a href="http://127.0.0.1:8000收到的报告如下：" target="_blank" rel="noopener">http://127.0.0.1:8000收到的报告如下：</a></p>
<p><img src="/HTTP-内容安全策略CSP学习笔记/1548838217202.png" alt="1548838217202"></p>
<h1 id="其他关键字"><a href="#其他关键字" class="headerlink" title="其他关键字"></a>其他关键字</h1><h1 id="兼容性"><a href="#兼容性" class="headerlink" title="兼容性"></a>兼容性</h1><p>毫无疑问很多浏览器是不支持这个字段的，详细的支持参见<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP" target="_blank" rel="noopener">https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP</a></p>
<h1 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h1><ul>
<li>内容安全策略( CSP )，<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP" target="_blank" rel="noopener">https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP</a></li>
<li>巧妙地绕过CSP：欺骗CSP执行任意代码，<a href="https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa" target="_blank" rel="noopener">https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa</a></li>
</ul>

    </div>

    
    
    
        
      
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Anemone</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://anemone.top/HTTP-内容安全策略CSP学习笔记/" title="内容安全策略CSP学习笔记">http://anemone.top/HTTP-内容安全策略CSP学习笔记/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/HTTP/" rel="tag"># HTTP</a>
            
              <a href="/tags/CSP/" rel="tag"># CSP</a>
            
              <a href="/tags/协议安全/" rel="tag"># 协议安全</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/ctf-2019fireshell一道从xxe到ssrf题/" rel="next" title="2019fireshell一道从XXE到SSRF题">
                  <i class="fa fa-chevron-left"></i> 2019fireshell一道从XXE到SSRF题
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/HTTP-跨域资源共享CORS学习笔记/" rel="prev" title="HTTP的访问控制与跨域资源共享（CORS）">
                  HTTP的访问控制与跨域资源共享（CORS） <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    <div class="comments" id="gitalk-container"></div>
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用"><span class="nav-number">2.</span> <span class="nav-text">使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#在HTTP头部指定策略"><span class="nav-number">2.1.</span> <span class="nav-text">在HTTP头部指定策略</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用meta标签指定策略"><span class="nav-number">2.2.</span> <span class="nav-text">使用meta标签指定策略</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#示例"><span class="nav-number">3.</span> <span class="nav-text">示例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#存在漏洞的服务器"><span class="nav-number">3.1.</span> <span class="nav-text">存在漏洞的服务器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#应用默认策略"><span class="nav-number">3.2.</span> <span class="nav-text">应用默认策略</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#信任多个域名"><span class="nav-number">3.3.</span> <span class="nav-text">信任多个域名</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#来源配置"><span class="nav-number">3.4.</span> <span class="nav-text">来源配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#精确控制"><span class="nav-number">3.5.</span> <span class="nav-text">精确控制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#内嵌脚本"><span class="nav-number">3.6.</span> <span class="nav-text">内嵌脚本</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#测试策略"><span class="nav-number">4.</span> <span class="nav-text">测试策略</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其他关键字"><span class="nav-number">5.</span> <span class="nav-text">其他关键字</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#兼容性"><span class="nav-number">6.</span> <span class="nav-text">兼容性</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考链接"><span class="nav-number">7.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Anemone">
  <p class="site-author-name" itemprop="name">Anemone</p>
  <div class="site-description" itemprop="description">关注Web安全、移动安全、Fuzz测试和机器学习</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/anemone95" title="GitHub &rarr; https://github.com/anemone95" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:anemone95@qq.com" title="E-Mail &rarr; mailto:anemone95@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
  </div>
  <div class="cc-license motion-element" itemprop="license">
    
  
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">anemone</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.4.0</div>

        






  
  <script>
  function leancloudSelector(url) {
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = visitors.getAttribute('id').trim();
      var title = visitors.getAttribute('data-flag-title').trim();

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .then(() => {
                leancloudSelector(url).innerText = counter.time + 1;
              })
            
              .catch(error => {
                console.log('Failed to save visitor count', error);
              })
          } else {
              Counter('post', '/classes/Counter', { title: title, url: url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.log('Failed to create', error);
                });
            
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return element.getAttribute('id').trim();
      });

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url: { '$in': entries } })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length === 0) {
            document.querySelectorAll('.leancloud_visitors .leancloud-visitors-count').forEach(element => {
              element.innerText = 0;
            });
            return;
          }
          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.url;
            var time = item.time;
            leancloudSelector(url).innerText = time;
          }
          for (var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = leancloudSelector(url);
            if (element.innerText == '') {
              element.innerText = 0;
            }
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=o5UaCJdPfEG0g7MVxXSMagpT-gzGzoHsz')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method: method,
          headers: {
            'X-LC-Id': 'o5UaCJdPfEG0g7MVxXSMagpT-gzGzoHsz',
            'X-LC-Key': 'c6IN1PuMV3QPltJcrHfn74Gt',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
        if (localhost.test(document.URL)) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>






        
      </div>
    </footer>
  </div>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script>
  <script src="https://cdn.bootcss.com/velocity/1.2.1/velocity.min.js"></script>
  <script src="https://cdn.bootcss.com/velocity/1.2.1/velocity.ui.js"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>
<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/instantsearch.js@2.10.4/dist/instantsearch.min.css">
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@2.10.4/dist/instantsearch.min.js"></script><script src="/js/algolia-search.js?v=7.4.0"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.bootcss.com/mermaid/8.2.6/mermaid.min.js', () => {
    mermaid.initialize({
      theme: 'forest',
      logLevel: 3,
      flowchart: { curve: 'linear' },
      gantt: { axisFormat: '%m/%d/%Y' },
      sequence: { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>




  

  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: 'f3075553d7b0225df6ca',
      clientSecret: '68362ba87c4cc8e13103afcf729f5bd8ea176a78',
      repo: 'anemone95.github.io',
      owner: 'Anemone95',
      admin: ['Anemone95'],
      id: 'f86f3792917371487b9484e953570ead',
        language: window.navigator.language || window.navigator.userLanguage,
      
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
</script>

</body>
</html>
